﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web.Script.Serialization;

namespace Kaiser.Models.JsonModels
{
    public class JsonComboBoxItem
    {
        public string id { get; set; }
        public string text { get; set; }
        public bool selected { get; set; }
    }
    public class JsonComboBox
    {
        private List<JsonComboBoxItem> List
        {
            get;
            set;
        }
        private DataTable _resultTable;
        private string ColumnValue, ColumnName, SelectValue;
        public JsonComboBox(DataTable dt, string columnname, string columnvalue,string selectvalue)
        {
            _resultTable = dt;
            ColumnName = columnname;
            ColumnValue = columnvalue;
            SelectValue = selectvalue;
            List = new List<JsonComboBoxItem>();
        }

        private void BuidData()
        {
            foreach (DataRow dr in _resultTable.Rows)
            {
                var item = new JsonComboBoxItem();
                item.id = dr[ColumnValue].ToString();
                item.text = dr[ColumnName].ToString();
                item.selected = dr[ColumnValue].ToString() == SelectValue;
                List.Add(item);
            }
        }

        public string ToJson()
        {
            BuidData();
            JavaScriptSerializer json = new JavaScriptSerializer();
            return json.Serialize(this.List);
        }
    }
}
